package zcw.com.lib_leet_code;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 朱城委 on 2021/7/13.<br><br>
 *
 * 汇总区间
 */
public class Topic228 {

    public static void main(String[] args) {
        Topic228 instance = new Topic228();

        int[] array = new int[] {0,1,2,4,5,7};
        System.out.println(instance.summaryRanges(array));

        array = new int[] {0,2,3,4,6,8,9};
        System.out.println(instance.summaryRanges(array));

        array = new int[] {};
        System.out.println(instance.summaryRanges(array));

        array = new int[] {-1};
        System.out.println(instance.summaryRanges(array));

        array = new int[] {0};
        System.out.println(instance.summaryRanges(array));
    }

    public List<String> summaryRanges(int[] nums) {
        List<String> result = new ArrayList<>();
        if(nums == null || nums.length == 0) {
            return result;
        }

        int start = 0;
        int end = 0;
        for(int i = 0; i < nums.length; i++) {
            if(i + 1 < nums.length && nums[i] + 1 == nums[i + 1]) {
                end++;
            }
            else {
                if(start == end) {
                    result.add(String.valueOf(nums[start]));
                }
                else {
                    result.add(nums[start] + "->" + nums[end]);
                }

                start = i + 1;
                end = start;
            }
        }

        return result;
    }
}
